package rssApp.components 
{
	import com.bit101.components.Label;
	import com.bit101.components.Panel;
	import com.bit101.components.PushButton;
	import com.bit101.components.Text;
	import com.bit101.components.VBox;
	import common.IDisposable;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.net.navigateToURL;
	import rssApp.enums.RssAppEventEnum;
	import rssApp.events.RssAppLinkEvent;
	/**
	 * Custom component for displaying an RSS feed entry. The component is 
	 * designed to fit into a fixed with, and will automatically adjust
	 * content vertically to fit. The component displays title, 
	 * description, date, category, and has a button which dispatches a
	 * RssAppLinkEvent when clicked.
	 * 
	 * The component has been coded to largly follow the style of the minimal
	 * comps components, with the exception of using a global event dispatcher
	 * for better integration with the rest of the RssApp code.
	 * 
	 * @author Luke Van In
	 */
	public class RssAppItemComponent extends Panel implements IDisposable
	{
		
		// private variables
		private var _eventDispatcher:IEventDispatcher;
		
		private var _layoutComponent:VBox;
		
		private var _titleComponent:Label;
		
		private var _descriptionComponent:MultilineLabelComponent;
		
		private var _dateComponent:Label;
		
		private var _categoryComponent:Label;
		
		private var _viewButton:PushButton;
		
		
		
		private var _title:String;
		
		private var _description:String;
		
		private var _date:String;
		
		private var _category:String;
		
		private var _link:String;
		
		
		
		
		
		
		/**
		 * Feed entry title
		 */
		public function set title(title:String):void
		{
			this._title = title;
			this.invalidate();
		}
		
		public function get title():String
		{
			return this._title;
		}
		
		
		
		
		/**
		 * Feed entry description
		 */
		public function set description(description:String):void
		{
			this._description = description;
			this.invalidate();
		}
		
		public function get description():String
		{
			return this._description;
		}
		
		
		
		
		/**
		 * Feed entry date
		 */
		public function set date(date:String):void
		{
			this._date = date;
			this.invalidate();
		}
		
		public function get date():String
		{
			return this._date;
		}
		
		
		
		
		/**
		 * Feed entry category
		 */
		public function set category(category:String):void
		{
			this._category = category;
			this.invalidate();
		}
		
		public function get category():String
		{
			return this._category;
		}
		
		
		
		
		/**
		 * URL of full article
		 */
		public function set link(link:String):void
		{
			this._link = link;
			this.invalidate();
		}
		
		public function get link():String
		{
			return this._link;
		}
		
		
		
		
		
		/**
		 * Constructor
		 * 
		 * @param	eventDispatcher		Global event router (RssAppLinkEvent events are dispatched through this event dispatcher)
		 * @param	parent
		 * @param	xpos
		 * @param	ypos
		 */
		public function RssAppItemComponent(eventDispatcher:IEventDispatcher, parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number =  0) 
		{
			super(parent, xpos, ypos);
			
			
			this._eventDispatcher = eventDispatcher;
			
			
			// layout
			this._layoutComponent = new VBox(this, 0, 0);
			
			this._layoutComponent.spacing = 0;
			
			
			// title
			this._titleComponent = new Label(this._layoutComponent, 0, 0);
			
			
			// description
			this._descriptionComponent = new MultilineLabelComponent(this._layoutComponent, 0, 0);
			
			
			// date
			this._dateComponent = new Label(this._layoutComponent, 0, 0);
			
			
			// category
			this._categoryComponent = new Label(this._layoutComponent, 0, 0);
			
			
			// view
			this._viewButton = new PushButton(this._layoutComponent, 0, 0, "Read more...", this.viewHandler);
			
			
			// force update
			this.invalidate();
		}
		
		
		
		
		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void
		{
			// set component values
			this._titleComponent.text = this._title;
			
			this._descriptionComponent.text = this._description;
			
			this._dateComponent.text = this._date;
			
			this._categoryComponent.text = this._category;
			
			
			// size component contents to width (description will 
			// automatically resize vertically to fit the required
			// width.
			this._descriptionComponent.width = this._width;
			
			this._descriptionComponent.draw();
			
			this._layoutComponent.draw();
			
			this._height = this._layoutComponent.height;
			
			
			// call default behavior
			super.draw();
		}
		
		
		
		
		/**
		 * Handles "read more" button click events.
		 * Dispatches an RssAppLinkEvent with the URL of the article. The 
		 * application should listen for the event and call the appropriate
		 * action, eg: navigateToUrl().
		 * 
		 * @param	e
		 */
		private function viewHandler(e:Event):void
		{
			var event:RssAppLinkEvent = new RssAppLinkEvent(RssAppEventEnum.LINK, this._link);
			
			this._eventDispatcher.dispatchEvent(event);
		}
		
		
		
		
		/* INTERFACE common.IDisposable */
		
		/**
		 * 
		 */
		public function dispose():void 
		{
			this._viewButton.dispose();
		}
		
		
		
	}

}